cssjshtml vue.js 跨域请求

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 3971

2019/01/10 15:05


1.fetch方法:

App.vue


  export default {
    name: 'App',
    data() {
      return {}
    },
    //fetch
    created() {
      fetch("/apis/test/testToken.php", {
        method:"post",
        headers:{
          token:"f4c9023jkwalkfjas2910a"
        },
        body:JSON.stringify(
          {username:"brownwang",password:"21232"}
          )
          .then(result =>{
            return result.json()
          })
          .then(data => {
            console.log(data)
          })
      })
    }
  }

/config/index.js

    proxyTable: {
      '/apis':{
        target:'https://goods.footer.com',  //接口名称
        changeOrigin:true,   //是否跨域
        pathRewrite:{
          '^/apis':''   //需要rewrite重写的
        }
      }
    },

2.axios

npm install axios

配置main.js

import axios from 'axios'

axios.defaults.headers.common['token']='12fdafsafdfsadfsaf'
axios.defaults.headers.post["Content-type"]="application/json"
Vue.prototype.$axios=axios

App.vue实现:

  export default {
    name: 'App',
    data() {
      return {}
    },
    //axios
    created() {

      this.$axios.post("/apis/test/testToken.php",
        {username:"brownwang",password:"1232"})
        .then(data=>{
          console.log(data)
        })

    }
  }

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1